package com.yzl.app.component;

import com.cunoraz.gifview.library.GifView;
import com.ryan.ohos.extension.EstimateHelper;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;


import java.io.IOException;
import java.util.ArrayList;


public class AnimatedProgressbar extends DirectionalLayout implements
        Component.EstimateSizeListener, ComponentContainer.ArrangeListener  {


    private int width =0;
    private int childWidth = 0;
    private GifView gifView;
    private Text text;
    float progress=0;
    int margin =0;
//    private ArrayList<>list;
    public AnimatedProgressbar(Context context) {
        super(context);
        setEstimateSizeListener(this);
        setArrangeListener(this);
    }

    public AnimatedProgressbar(Context context, AttrSet attrSet) {
        super(context,attrSet);
        setEstimateSizeListener(this);
        setArrangeListener(this);
    }

    public AnimatedProgressbar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        setEstimateSizeListener(this);
        setArrangeListener(this);

    }

    @Override
    public boolean onEstimateSize(int widthSpec, int heightSpec) {
        int childCount = getChildCount();
        int paddingHor = getPaddingLeft() + getPaddingRight();
        int paddingVer = getPaddingTop() + getPaddingBottom();
        for (int i = 0; i < childCount; i++) {
            Component child = getComponentAt(i);
            margin+=child.getMarginLeft()+child.getMarginRight();
            LayoutConfig lc = (LayoutConfig) child.getLayoutConfig();
            int childWidthSpec = EstimateHelper.getChildMeasureSpec(widthSpec, paddingHor, lc.width);
            int childHeightSpec = EstimateHelper.getChildMeasureSpec(heightSpec, paddingVer, lc.height);
            child.estimateSize(childWidthSpec, childHeightSpec);
        }

        int width = EstimateSpec.getSize(widthSpec);
        this.width =width;
//如果要用mode测量就用这个
//        int wideMode = EstimateSpec.getMode(widthEstimateConfig);
        int height = EstimateSpec.getSize(heightSpec);
        setEstimatedSize(width, height);
        if (progress!=0){
            setProgress(progress);
        }
        return true;
    }

    @Override
    public boolean onArrange(int l, int t, int width, int height) {
        int childCount = getChildCount();
        int childTop = t;
        childWidth =0;
        int positionX =0;
        for(int i = 0; i < childCount; i++) {
            Component child = getComponentAt(i);

            if (i==1){
                child.arrange(positionX, 0, child.getEstimatedWidth(), child.getEstimatedHeight());
            }if (i==0){
                child.arrange(child.getMarginLeft(), 0, child.getEstimatedWidth(), child.getEstimatedHeight());

            }
            positionX =positionX+child.getMarginLeft()+child.getMarginRight()+child.getEstimatedWidth();
//            childTop += childHeight;

            if (i == 0) {
//                maxDeltaY = -childHeight;
            }
            HiLog.info(new HiLogLabel(0,0,"childsize>>>>"), ""+child.getEstimatedWidth(), ""+child.getEstimatedWidth());
            if (i==0||i==1||i==3){
                childWidth+=child.getEstimatedWidth();
            }
        }
        HiLog.info(new HiLogLabel(0,0,"childsize>>>>"), "", ""+width);
        return false;
    }


   public void setTextColor(Color color){
       ((Text)getComponentAt(0)).setTextColor(color);
   }
    public void setTextSize(int size){
        ((Text)getComponentAt(0)).setTextSize(size);
        invalidate();
    }

    public void setPercentTextAndBarMargin(int marginVp){
        getComponentAt(1).setWidth(AttrHelper.vp2px(marginVp,getContext()));
        invalidate();
    }
    //初始化child
    public void init(int percentTextWidthVp, int percentTextHeightVp, RgbColor barColor,int barHeightVp,String gif,int gifWidthVp,int gifHeightVp){

        DirectionalLayout.LayoutConfig textLayoutConfig = new DirectionalLayout.LayoutConfig();
        //百分比
        Text percentText =  new Text(getContext());
        int perWidth = AttrHelper.vp2px(percentTextWidthVp,getContext());
        int perHeight = AttrHelper.vp2px(percentTextHeightVp,getContext());
        textLayoutConfig.width = perWidth;
        textLayoutConfig.height = perHeight;

        percentText.setLayoutConfig(textLayoutConfig);


        //进度条
        DirectionalLayout.LayoutConfig barLayoutConfig = new DirectionalLayout.LayoutConfig();
        Text barText =  new Text(getContext());
        barText.setText("");
        int barHeight = AttrHelper.vp2px(barHeightVp,getContext());
        barLayoutConfig.height = barHeight;
        barLayoutConfig.width=500;

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setCornerRadius(5);
        shapeElement.setRgbColor(barColor);
        barText.setBackground(shapeElement);
        barText.setLayoutConfig(barLayoutConfig);


        GifViewJava gifView = new GifViewJava(getContext(),gif);
        gifView.setScaleMode(Image.ScaleMode.ZOOM_CENTER);

        DirectionalLayout.LayoutConfig gifLayoutConfig = new DirectionalLayout.LayoutConfig();
        int gifWidth = AttrHelper.vp2px(gifWidthVp,getContext());
        int gifHeight = AttrHelper.vp2px(gifHeightVp,getContext());
        gifLayoutConfig.width = gifWidth;
        gifLayoutConfig.height = gifHeight;

        gifView.setLayoutConfig(gifLayoutConfig);

        DirectionalLayout.LayoutConfig marginLayoutConfig = new DirectionalLayout.LayoutConfig();
        marginLayoutConfig.width =10;
        marginLayoutConfig.height=1;
        Text margin = new Text(getContext());
        margin.setLayoutConfig(marginLayoutConfig);
        addComponent(percentText,0);
        addComponent(margin,1);
        addComponent(barText,2);
        addComponent(gifView,3);

    }

    public void setImg(String res){

        ResourceManager resourceManager = this.getContext().getResourceManager();
        RawFileEntry rawFileEntry=resourceManager.getRawFileEntry(res);
        gifView = (GifView)getComponentAt(2);
        try {
            gifView.load(rawFileEntry);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void setProgress(float progress){
        this.progress = progress;
        text = (Text)getComponentAt(0);
        text.setText(progress+"%");

        getComponentAt(2).setWidth((int)((width-childWidth-margin)*(progress/100)));

        invalidate();

    }



}
